<template>
  <div>
    <div class="filter-container">
      <el-button @click="handelAdd" type="button" class="el-button filter-item el-button--primary el-button--mini">
        <i class="el-icon-edit"></i><span>添加</span>
      </el-button>
    </div>
    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border
      :tree-props="{ children: 'children' }" v-loading="loading">
      <el-table-column prop="id" label="类目ID" min-width="180" align="center">
      </el-table-column>
      <el-table-column prop="name" label="类目名" min-width="180" align="center">
      </el-table-column>
      <el-table-column prop="picUrl" label="类目图标" min-width="180" align="center">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image style="width: 40px;" :src="scope.row.iconUrl" :preview-src-list="[scope.row.iconUrl]">
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="picUrl" label="类目图片" min-width="180" align="center">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image style="width: 90px; height: 40px" :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]">
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="keywords" label="关键字" min-width="180" align="center">
      </el-table-column>
      <el-table-column prop="desc" label="简介" min-width="180" align="center">
      </el-table-column>
      <el-table-column prop="level" label="级别" min-width="180" align="center">
        <template slot-scope="scope">
          <span :class="scope.row.level === 'L1' ? 'level-l1' : (scope.row.level === 'L2' ? 'level-l2' : '')">
            {{ getDisplayText(scope.row.level) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="200px" align="center">
        <template slot-scope="scope">
          <el-button @click="handeEdite(scope.row)" type="button"
            class="el-button el-button--primary el-button--mini"><span>编辑</span></el-button>
          <el-button @click="handleDel(scope.row)" type="button"
            class="el-button el-button--danger el-button--mini"><span>删除</span></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑 -->
    <el-dialog title="用户编辑" :visible.sync="dialogVisible" width="50%">
      <div class="conter">
        <el-form :model="ListQuery" ref="ListQuery" label-width="100px" class="demo-ListQuery" :data="ListQuery">
          <el-form-item label="类目" prop="name">
            <el-input v-model="ListQuery.name"></el-input>
          </el-form-item>
          <el-form-item label="关键字" prop="keywords">
            <el-input v-model="ListQuery.keywords"></el-input>
          </el-form-item>
          <el-form-item label="级别">
            <el-select v-model="ListQuery.level">
              <el-option label="一级类目" value="L1"></el-option>
              <el-option label="二级类目" value="L2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类目图标" prop="content">
            <el-upload class="avatar-uploader" :action="uploadPath" :show-file-list="false" :on-success="handleSuccess">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类目图片" prop="content">
            <el-upload class="avatar-uploader" :action="uploadPath" :show-file-list="false"
              :on-success="handleAvatarSuccess">
              <img v-if="imageUrls" :src="imageUrls" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类目简介" prop="desc">
            <el-input v-model="ListQuery.desc"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增 -->
    <el-dialog title="添加" :visible.sync="Visible" width="50%">
      <div class="content">
        <el-form :model="getQuery" label-width="100px" class="demo-ListQuery">
          <el-form-item label="类目" prop="name">
            <el-input v-model="getQuery.name"></el-input>
          </el-form-item>
          <el-form-item label="关键字" prop="keywords">
            <el-input v-model="getQuery.keywords"></el-input>
          </el-form-item>
          <el-form-item label="级别">
            <el-select v-model="getQuery.level">
              <el-option label="一级类目" value="L1"></el-option>
              <el-option label="二级类目" value="L2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类目商标" prop="mobile">
            <el-upload class="avatar-uploader" :show-file-list="false" :action="uploadPath"
              :on-success="handleSuccessOne">
              <img v-if="handelUrl" :src="handelUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类目图片" prop="mobile">
            <el-upload class="avatar-uploader" :show-file-list="false" :action="uploadPath"
              :on-success="handleSuccessTwo">
              <img v-if="handelUrls" :src="handelUrls" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类目简介" prop="desc">
            <el-input v-model="getQuery.desc"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="Visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForms()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { listCategory, createCategory, updateCategory, deleteCategory, uploadPath } from '@/api/category'
export default {
  data() {
    return {
      uploadPath,
      srcList: [],
      url: '',
      imageUrl: '',
      imageUrls: '',
      handelUrl: '',
      handelUrls: '',
      loading: false,
      dialogVisible: false,
      Visible: false,
      tableData: [],
      ListQuery: [],
      getQuery: {
        name: '',
        keywords: '',
        level: '',
        desc: '',
        iconUrl: '',
        picUrl: '',
        pid: 0
      },
    }
  },
  methods: {
    handleSuccessOne(response, file, fileList) {
      this.handelUrl = response.data.url
    },
    handleSuccessTwo(response) {
      this.handelUrls = response.data.url
    },
    // 添加
    submitForms() {
      this.getQuery.iconUrl = this.handelUrl;
      this.getQuery.picUrl = this.handelUrls;
      createCategory(this.getQuery).then(() => {
        this.$message({
          message: '添加成功',
          type: 'success'
        });
        this.Visible = false
        this.handleGetList()
      })
    },
    // 新增
    handelAdd() {
      this.Visible = true;
    },
    // 确定
    submitForm() {
      this.ListQuery.iconUrl = this.imageUrl
      this.ListQuery.picUrl = this.imageUrls
      updateCategory(
        this.ListQuery
      ).then(() => {
        this.dialogVisible = false
        this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.handleGetList();
      })
    },
    // 成功后的参数
    handleSuccess(response, file, fileList) {
      this.imageUrl = response.data.url
      console.log(response)
    },
    handleAvatarSuccess(response, file, fileList) {
      this.imageUrls = response.data.url
      console.log(response)
    },
    // 详情
    handeEdite(row) {
      // 深拷贝
      this.ListQuery = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true
      this.imageUrl = row.iconUrl
      this.imageUrls = row.picUrl
    },
    // 删除
    handleDel(row) {
      this.$confirm('此操作将删除数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCategory({ id: row.id }).then(res => {
          if (!res) { return }
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.handleGetList()
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 处理级别
    getKeywordStyle(keywords) {
      if (keywords == 'L1') {
        return {
          color: 'blue',
          fontWeight: 'bold',
          textDecoration: 'underline'
        }
      } else if (keywords == 'L2') {
        return {
          color: 'gray',
          fontStyle: 'italic',
          backgroundColor: '#f0f0f0'
        }
      }
      return {}
    },
    getDisplayText(keywords) {
      if (keywords == 'L1') {
        return `一级类目`
      } else if (keywords == 'L2') {
        return `二级类目`
      }
      return keywords
    },
    // 获取列表
    handleGetList() {
      this.loading = true;
      listCategory({}).then(res => {
        // console.log(res.data.list);
        this.tableData = res.data.list
        this.loading = false;
      })
    },
  },
  mounted() {
    this.handleGetList()
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.level-l1 {
  height: 20px;
  padding: 0 5px;
  line-height: 19px;
  background-color: #e8f4ff;
  border-color: #d1e9ff;
  display: inline-block;
  font-size: 14px;
  color: #1890ff;
  border-width: 1px;
  border-style: solid;
  border-radius: 4px;
  box-sizing: border-box;
  white-space: nowrap;
}

.level-l2 {
  height: 20px;
  padding: 0 5px;
  line-height: 19px;
  display: inline-block;
  font-size: 14px;
  border-width: 1px;
  border-style: solid;
  border-radius: 4px;
  box-sizing: border-box;
  white-space: nowrap;
  background-color: #f4f4f5;
  border-color: #e9e9eb;
  color: #909399;
}

.filter-container {
  padding-bottom: 10px;
}
</style>